昨天已經學會新增頁面和跳頁功能了
但如果單純跳頁好像沒什麼用
勢必要傳一點訊息過去另一頁
今天就來學
從第一頁傳你輸入的字到第二頁
用TextView顯示出來
Let's 走
我們直接拿昨天的專案
第一頁:新增EditText
第二頁:新增TextView
拉一個EditText出來
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_totwo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    <Button
        android:id="@+id/btn_totwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳第二頁"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
這裡要比昨天多一個程式碼
intent.putExtra();
intent.putExtra("send",et_input.getText().toString());
完整程式:
package com.example.intent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
    Button btn_totwo;
    EditText et_input;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_totwo = findViewById(R.id.btn_totwo);
        btn_totwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,Two.class);
                et_input = findViewById(R.id.et_input);
                intent.putExtra("send",et_input.getText().toString());
                startActivity(intent);
            }
        });
    }
}
拉一個TextView顯示傳過來的結果
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Two"
    android:background="@color/black">
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:textSize="30dp"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/btn_back"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳回去"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
第一頁傳了一個訊息
那這一頁勢必要接剛剛傳過來的訊息
intent.getXXXExtra(); XXX是你剛剛傳過來的資料型別名稱
剛剛是傳字串過來
所以這邊用String
接收的程式碼:
Intent intent = getIntent();
String send = intent.getStringExtra("send");//send為上一頁設的name
完整程式:
package com.example.intent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextClock;
import android.widget.TextView;
public class Two extends AppCompatActivity {
    Button btn_back;
    TextView tv_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        tv_result = findViewById(R.id.tv_result);
        btn_back = findViewById(R.id.btn_back);
        Intent intent = getIntent();
        String send = intent.getStringExtra("send");
        tv_result.setText(send);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
執行結果:
Intent其實有非常多功能
開啟其他APP,也是用Intent的喔
30天挑戰終於結束了
Android Studio還有很多功能可以學習
有興趣的網路上有一堆教學影片或文章
可以供大家學習
就在這邊跟大家說:
掰掰摟![]()
![]()